import 'dart:async';
import 'dart:io';

import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:easy_log/easy_log_native.dart';
import 'package:easy_log/easy_log.dart';
import 'package:sqflite/sqflite.dart';
import 'package:sqflite_common_ffi/sqflite_ffi.dart';

void main() {
  const MethodChannel channel = MethodChannel('easy_log');

  TestWidgetsFlutterBinding.ensureInitialized();

  setUp(() {
    channel.setMockMethodCallHandler((MethodCall methodCall) async {
      return '42';
    });
  });

  tearDown(() {
    channel.setMockMethodCallHandler(null);
  });

  test('getPlatformVersion', () async {
    expect(await EasyLog.platformVersion, '42');
  });

  group('test log', () {
    setUpAll(() {
      sqfliteFfiInit();
      databaseFactory = databaseFactoryFfi;
      var dir = Directory('.dart_tool/sqflite_common_ffi/databases/test');
      print('path: ${dir.path}');
      if (dir.existsSync()) {
        dir.deleteSync(recursive: true);
      }
    });
    test('test', () async {
      EasyLoggerFactory.isTestMode = true;
      await EasyLoggerFactory.instance.init();
      var defaultLogger = EasyLoggerFactory.instance.logger();
      // var namedLogger = EasyLoggerFactory.instance.logger(logName: 'test');
      defaultLogger.debug('defaultLogger');
      // namedLogger.error('namedLogger');
    }, skip: true);

    test('test class log', () async {
      EasyLoggerFactory.isTestMode = true;
      await EasyLoggerFactory.instance.init(
          appName: 'shop', appDate: DateTime.now(), appVersion: [1, 0, 1]);
      testLogFunction();
      TestLogOutput.staticPrint();
      TestLogOutput().dynamicPrint();
      TestLogMulti.staticPrint();
      TestLogMulti()
        ..dynamicPrint()
        ..dispose();
      TestPrintAllLevel();
      await EasyLoggerFactory.instance.unInit();
      Completer completer = Completer();
      Timer(Duration(seconds: 200), () {
        print('finished.');
        completer.complete();
      });
      return completer.future;
    });
  });
}

void testLogFunction() {
  // static var defaultLogger = EasyLoggerFactory.instance.logger();
  var defaultLogger = gEasyLogger(); // simple
  defaultLogger.warning('testLogFunction');
  void internalFunction() {
    defaultLogger.warning('internalFunction');
  }

  internalFunction();
  Timer(Duration(), () {
    defaultLogger.warning('anonymity');
  });
}

class TestLogOutput {
  // static var defaultLogger = EasyLoggerFactory.instance.logger();
  static var defaultLogger = gEasyLogger(); // simple
  static void staticPrint() {
    defaultLogger.debug('defaultLogger');
  }

  void dynamicPrint() {
    defaultLogger.debug('defaultLogger');
  }
}

class TestLogMulti {
  // static var namedLogger = EasyLoggerFactory.instance.logger(logName: 'im');
  static var namedLogger = gEasyNamedLogger('im'); // simple
  static void staticPrint() {
    namedLogger.debug('namedLogger');
  }

  void dynamicPrint() {
    namedLogger.debug('namedLogger');
  }

  void dispose() {
    namedLogger.dispose();
  }
}

void TestPrintAllLevel() {
  var defaultLogger = gEasyLogger(); // simple
  defaultLogger.verbose('verbose');
  defaultLogger.debug('debug');
  defaultLogger.info('info');
  defaultLogger.warning('warning');
  defaultLogger.error('error');
  defaultLogger.fatal('fatal');
  defaultLogger.crash('crash');
}
